package xhwl.hmo.mall.ui;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewCompat;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;

import xhwl.hmo.mall.BaseFragmentActivity;
import xhwl.hmo.mall.R;
import xhwl.hmo.mall.ui.fragment.AllOrderFragment;
import xhwl.hmo.mall.ui.fragment.CommentWaitingFragment;
import xhwl.hmo.mall.ui.fragment.DeliverGoodsWaitingFragment;
import xhwl.hmo.mall.ui.fragment.PayWaitingFragment;
import xhwl.hmo.mall.ui.fragment.ReceiveGoodsWaitingFragment;


public class MyOrderListActivity extends BaseFragmentActivity implements View.OnClickListener {
    public static final String EXTRA_TAB_INDEX = "tabIndex";
    View indicator;
    private ArrayList<Fragment> fragments;
    public static MyOrderListActivity instance;
    public int tabIndex;
    private TextView order_all;
    private TextView pay_waiting;
    private TextView delivery_good_waiting;
    private TextView take_delivery_good_waiting;
    private TextView comment_waiting;
    private ViewPager myViewpager;
    private AllOrderFragment allOrderFragment;
    private PayWaitingFragment payWaitingFragment;
    private DeliverGoodsWaitingFragment deliverGoodsWaitingFragment;
    private ReceiveGoodsWaitingFragment receiveGoodsWaitingFragment;
    private CommentWaitingFragment commentWaitingFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        tabIndex = getIntent().getIntExtra(EXTRA_TAB_INDEX, 0);
        setContentView(R.layout.activity_myorder);
        instance = this;
    }

    @Override
    public void initViews() {
        initTitleViews();
        titleTextView.setText("我的订单");
        indicator = (View) findViewById(R.id.indicator);
        order_all = (TextView) findViewById(R.id.order_all);
        pay_waiting = (TextView) findViewById(R.id.pay_waiting);
        delivery_good_waiting = (TextView) findViewById(R.id.delivery_good_waiting);
        take_delivery_good_waiting = (TextView) findViewById(R.id.take_delivery_good_waiting);
        comment_waiting = (TextView) findViewById(R.id.comment_waiting);
        myViewpager = (ViewPager) findViewById(R.id.viewpager);
        initData();
    }

    @Override
    public void initListener() {
        order_all.setOnClickListener(this);
        pay_waiting.setOnClickListener(this);
        delivery_good_waiting.setOnClickListener(this);
        take_delivery_good_waiting.setOnClickListener(this);
        comment_waiting.setOnClickListener(this);
    }

    @Override
    public void loadData() {

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.order_all:
                myViewpager.setCurrentItem(0);
                break;
            case R.id.pay_waiting:
                myViewpager.setCurrentItem(1);
                break;
            case R.id.delivery_good_waiting:
                myViewpager.setCurrentItem(2);
                break;
            case R.id.take_delivery_good_waiting:
                myViewpager.setCurrentItem(3);
                break;
            case R.id.comment_waiting:
                myViewpager.setCurrentItem(4);
                break;
        }
    }

    public void initData() {
        fragments = new ArrayList<Fragment>();
        fragments.clear();
//        if (null != allOrderFragment) {
//            fragments.add(allOrderFragment);
//        } else {
//            allOrderFragment = new AllOrderFragment();
//            fragments.add(allOrderFragment);
//        }
//        if (null != payWaitingFragment) {
//            fragments.add(payWaitingFragment);
//        } else {
//            payWaitingFragment = new PayWaitingFragment();
//            fragments.add(payWaitingFragment);
//        }
//        if (null != deliverGoodsWaitingFragment) {
//            fragments.add(deliverGoodsWaitingFragment);
//        } else {
//            deliverGoodsWaitingFragment = new DeliverGoodsWaitingFragment();
//            fragments.add(deliverGoodsWaitingFragment);
//        }
//        if (null != receiveGoodsWaitingFragment) {
//            fragments.add(receiveGoodsWaitingFragment);
//        } else {
//            receiveGoodsWaitingFragment = new ReceiveGoodsWaitingFragment();
//            fragments.add(receiveGoodsWaitingFragment);
//        }
//        if (null != commentWaitingFragment) {
//            fragments.add(commentWaitingFragment);
//        } else {
//            commentWaitingFragment = new CommentWaitingFragment();
//            fragments.add(commentWaitingFragment);
//        }
        fragments.add(new AllOrderFragment());
        fragments.add(new PayWaitingFragment());
        fragments.add(new DeliverGoodsWaitingFragment());
        fragments.add(new ReceiveGoodsWaitingFragment());
        fragments.add(new CommentWaitingFragment());
        //1.给ViewPager填充Fragment
        FragmentPagerAdapter fragmentAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return fragments.get(position);
            }

            @Override
            public int getCount() {
                return fragments.size();
            }
        };
        myViewpager.setAdapter(fragmentAdapter);
        updateTitle();
        //初始化指示线的宽度
        initIndicatorLine();
        intIndicator();
        myViewpager.setCurrentItem(tabIndex);
    }

    private void initIndicatorLine() {
        //1.计算线的宽度
        int width = getWindowManager().getDefaultDisplay().getWidth() / fragments.size();
        //2.将width设置给indicator
        ViewGroup.LayoutParams params = indicator.getLayoutParams();
        params.width = width;
        indicator.setLayoutParams(params);
    }

    private void intIndicator() {
        myViewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            /*
         * 当滑动ViewPager的时候会执行该方法
         * @param position 当前选中的位置
         * @param positionOffset 表示当前手指滑动距离的百分比
         * @param positionOffsetPixels 表示当前手指滑动的距离，单位是像素
         */
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                //1.计算线需要滚动到的距离
                float targetX = indicator.getWidth() * position + positionOffsetPixels / fragments.size();
                //2.让线滚动到指定的距离
                ViewCompat.setTranslationX(indicator, targetX);
            }

            @Override
            public void onPageSelected(int position) {
                updateTitle();
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void updateTitle() {
        //1.获取当前ViewPager选择的页
        int currentItem = myViewpager.getCurrentItem();
        order_all.setSelected(currentItem == 0);
        pay_waiting.setSelected(currentItem == 1);
        delivery_good_waiting.setSelected(currentItem == 2);
        take_delivery_good_waiting.setSelected(currentItem == 3);
        comment_waiting.setSelected(currentItem == 4);
        order_all.setTextColor(this.getResources().getColor(R.color.text_color_33));
        pay_waiting.setTextColor(this.getResources().getColor(R.color.text_color_33));
        delivery_good_waiting.setTextColor(this.getResources().getColor(R.color.text_color_33));
        take_delivery_good_waiting.setTextColor(this.getResources().getColor(R.color.text_color_33));
        comment_waiting.setTextColor(this.getResources().getColor(R.color.text_color_33));
        if (currentItem == 0) {
            order_all.setTextColor(this.getResources().getColor(R.color.tab_text_select));
        } else if (currentItem == 1) {
            pay_waiting.setTextColor(this.getResources().getColor(R.color.tab_text_select));
        } else if (currentItem == 2) {
            delivery_good_waiting.setTextColor(this.getResources().getColor(R.color.tab_text_select));
        } else if (currentItem == 3) {
            take_delivery_good_waiting.setTextColor(this.getResources().getColor(R.color.tab_text_select));
        } else if (currentItem == 4) {
            comment_waiting.setTextColor(this.getResources().getColor(R.color.tab_text_select));
        }
    }
}
